// pages/car/startcar/index.ts
import {save,WorkOrder,WorkOrderUser} from '../../../api/work_order'
import { LENGYU_SUCCESS_CODE } from '../../../utils/request';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    needItems:[ 

    ],
    showActionsheet: false,
    error:"",
    formData: {
      orderType:"",
    },
    procUsers:[],
    userDialogShow:false,
    rules: [
      {
        name: 'title',
        rules: {required: true, message: '请填写工单名称'},
      },
      {
        name: 'orderType',
        rules: {required: true, message: '请选择工单类型'},
      },
      {
          name: 'deviceType',
          rules: {required: true, message: '请选择设备类型'},
      },
      {
          name: 'priority',
          rules: {required: true, message: '请选择紧急程度'},
      },
      {
        name: 'deadlineDate',
        rules: {required: true, message: '请选择截止日期'},
    },
      // {
      //     name: 'deadlineTime',
      //     rules: {required: true, message: '请选择截止时间'},
      // },
      {
        name: 'procUserIds',
        rules: {required: true, message: '请选择处理人员'},
      },
      {
        name: 'instruction',
        rules: {required: true, message: '请填写问题描述'},
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  showActionSheet(showType:number){
    if(showType==1){
      //对应需求的选择
    }
    this.setData({
      showActionsheet: true
  })
  },
  actionSheetClick(e:any){
    console.log(e);
    this.setData({
      'formData.need': e.detail.value
    })
    console.log(this.data.formData)
    this.closeActionSheet();
  },
  showUserSelector(){
    this.setData({
      userDialogShow: true
    })
  },
  closeActionSheet() {
    this.setData({
        showActionsheet: false
    })
  },
  bindDateChange(e:any){
    this.setData({
      'formData.deadlineDate':e.detail.value
    })
  },
  bindTimeChange(e:any){
    this.setData({
      'formData.deadlineTime':e.detail.value
    })
  },
  orderTypeChanged(e:any){
    console.log("工单类型变更",e)
    this.setData({
      'formData.orderType':e.detail.value,
    })
  },
  bindTitleInput(e:any){
    this.setData({
      'formData.title':e.detail.value,
    })
  },
  deviceTypeChanged(e:any){
    console.log("设备类型变更",e)
    this.setData({
      'formData.deviceType':e.detail.value,
    })
  },
  priorityChanged(e:any){
    this.setData({
      'formData.priority':e.detail.value,
    })
  },

  bingProvideChange(e:any){
    this.setData({
      'formData.instruction':e.detail.value
    })
  },
  // bindUserSeleted(e:any){
  //   console.log("处理人员选择事件",e)
  //   this.setData({
  //     'procUsers':this.data.procUsers.concat(e.detail.user)
  //   })
  // },
  imgUrlListChanged(e:any){
    console.log("处理现场照片变更",e)
    this.setData({
      'formData.imgUrlList':(e.detail.imgUrlList.join(','))
    })
  },
  userListChanged(e:any){
    console.log("处理现场照片变更",e)
    let userIdsStr = ""
    if(e.detail.userList){
      e.detail.userList.forEach((item,index) => {
        if(index == 0){
          userIdsStr += item.id;
        }
        else{
          userIdsStr += ",";
          userIdsStr += item.id;
        }
      });
    }

    this.setData({
      'procUsers':e.detail.userList,
      'formData.procUserIds':userIdsStr
    })
  },
 async submitForm(){
   console.log("校验信息：" ,this.data.formData)
    this.selectComponent('#form').validate( async (valid:any, errors:any) =>{
      console.log('valid', valid, errors)
      if (!valid) {
          const firstError = Object.keys(errors)
          if (firstError.length) {
              this.setData({
                  error: errors[firstError[0]].message
              })
              wx.showToast({
                title: this.data.error,
                icon: 'error',
                duration: 2000
              })
          }
      } else {
        console.log(this.data.formData)
         let result = await save(this.data.formData as WorkOrder,this.data.procUsers as Array<WorkOrderUser>);
         console.log(result);
         if(result.code==LENGYU_SUCCESS_CODE){
          wx.navigateBack()
         }
      }
  })
  }
})